
#ifndef _LOG2_H_
#define _LOG2_H_

#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>

#include "log/log2_config.h"

void log_init(void);
void log_reset_level(void);
void log_write(const void* buf, int len);
void log_write_s(const char* str);
void log_printf(const char * fmt, ...);
void log_print_log(uint8_t level, const char* file, int line, const char* func, const char* fmt, ...);
void log_print_buf(uint8_t level, const char* file, int line, const char* func, const char* tag, const void* buf, int len);
void log_dump_hex(const void* buf, int len, int align);
void log_time(void);

#if (LOG2_CFG_LEVEL >= LOG2_LEVEL_ERR)

#define LOG2_PRINT(fmt, ...)              log_printf(fmt, ## __VA_ARGS__)
#define LOG2_WRITE(_buf, _len)       log_write(_buf, _len)
#define LOG2_WRITE_STR(_s)           log_write_s(_s)
#define LOG2_DUMP_HEX(_buf, _len)    log_dump_hex(_buf, _len, 16)
#define LOG2_TIME()                  log_time()
#else

#define LOG2_PRINT(fmt, ...)              ((void)0)
#define LOG2_WRITE(_buf, _len)               ((void)0)
#define LOG2_WRITE_STR(_s)           ((void)0)
#define LOG2_DUMP_HEX(_buf, _len)    ((void)0)
#define LOG2_TIME()                  ((void)0)
#endif

#define LOG2_CONTROL(ctrl)   do{LOG2_WRITE_STR(ctrl); log_reset_level();}while(0)
#define LOG2_CLEAR()     LOG2_CONTROL(LOG2_CTRL_CLEAR)

#define LOG2_FILE        ((LOG2_FILE_EN) > 0 ? __FILE__ : NULL)
#define LOG2_LINE        ((LOG2_LINE_EN) > 0 ? __LINE__ : 0)
#define LOG2_FUNC        ((LOG2_FUNC_EN) > 0 ? __FUNCTION__ : NULL)

#if LOG2_CFG_LEVEL  == LOG2_LEVEL_NONE
#define LOG2_RESET_LEVEL()		((void)0)
#else
#define LOG2_RESET_LEVEL()		log_reset_level()
#endif

#if (LOG2_CFG_LEVEL >= LOG2_LEVEL_DBG)

#define LOG2_DBG(fmt, ...)               log_print_log(LOG2_LEVEL_DBG, LOG2_FILE, LOG2_LINE, LOG2_FUNC, fmt, ## __VA_ARGS__)
#define LOG2_BUF_DBG(tag, buf, len)      log_print_buf(LOG2_LEVEL_DBG, LOG2_FILE, LOG2_LINE, LOG2_FUNC, tag, buf, len)

#else

#define LOG2_DBG(fmt, ...)   ((void)0)
#define LOG2_BUF_DBG(tag, buf, len)  ((void)0)

#endif //(LOG2_CFG_LEVEL >= LOG2_LEVEL_DBG)

#if (LOG2_CFG_LEVEL >= LOG2_LEVEL_INF)

#define LOG2_INF(fmt, ...)              log_print_log(LOG2_LEVEL_INF, LOG2_FILE, LOG2_LINE, LOG2_FUNC, fmt, ## __VA_ARGS__)
#define LOG2_BUF_INF(tag, buf, len)     log_print_buf(LOG2_LEVEL_INF, LOG2_FILE, LOG2_LINE, LOG2_FUNC, tag, buf, len)

#else

#define LOG2_INF(fmt, ...)   ((void)0)
#define LOG2_BUF_INF(tag, buf, len)  ((void)0)

#endif //(LOG2_CFG_LEVEL >= LOG2_LEVEL_INF)

#if (LOG2_CFG_LEVEL >= LOG2_LEVEL_WRN)

#define LOG2_WRN(fmt, ...)               log_print_log(LOG2_LEVEL_WRN, LOG2_FILE, LOG2_LINE, LOG2_FUNC, fmt, ## __VA_ARGS__)
#define LOG2_BUF_WRN(tag, buf, len)      log_print_buf(LOG2_LEVEL_WRN, LOG2_FILE, LOG2_LINE, LOG2_FUNC, tag, buf, len)

#else

#define LOG2_WRN(fmt, ...)   ((void)0)
#define LOG2_BUF_WRN(tag, buf, len)  ((void)0)

#endif //(LOG2_CFG_LEVEL >= LOG2_LEVEL_WRN)

#if (LOG2_CFG_LEVEL >= LOG2_LEVEL_ERR)

#define LOG2_ERR(fmt, ...)               log_print_log(LOG2_LEVEL_ERR, LOG2_FILE, LOG2_LINE, LOG2_FUNC, fmt, ## __VA_ARGS__)
#define LOG2_BUF_ERR(tag, buf, len)      log_print_buf(LOG2_LEVEL_ERR, LOG2_FILE, LOG2_LINE, LOG2_FUNC, tag, buf, len)

#else

#define LOG2_ERR(fmt, ...)  ((void)0)
#define LOG2_BUF_ERR(tag, buf, len)  ((void)0)

#endif //(LOG2_CFG_LEVEL >= LOG2_LEVEL_ERR)



#endif
